package com.lzq.rabbitmqdemo.amqp.controller;

import com.lzq.rabbitmqdemo.amqp.constant.Constants;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/producer")
public class ProducerController {
    @Autowired
    private RabbitTemplate rabbitTemplate;

    @RequestMapping("/work")
    public String work() {
        for (int i = 0; i < 10; i++) {
            rabbitTemplate.convertAndSend("", Constants.WORK_QUEUE,
                    "hello spring amqp: work...");
        }
        return "ok";
    }

    @RequestMapping("/fanout")
    public String fanout() {
        rabbitTemplate.convertAndSend(Constants.FANOUT_EXCHANGE_NAME, "",
                    "hello spring amqp: fanout...");
        return "ok";
    }

    @RequestMapping("/direct")
    public String direct(String routingKey) {
        rabbitTemplate.convertAndSend(Constants.DIRECT_EXCHANGE_NAME, routingKey,
                "hello spring amqp: direct..."+routingKey);
        return "ok";
    }

    @RequestMapping("/topic")
    public String topic(String routingKey) {
        rabbitTemplate.convertAndSend(Constants.TOPICS_EXCHANGE_NAME, routingKey,
                "hello spring amqp: topic..."+routingKey);
        return "ok";
    }
}
